package com.twm.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.twm.drug.common.ResultMapUtils;
import com.twm.drug.entity.Owinfo;
import com.twm.drug.service.IOwinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 药品出入库相关的controller
 */
@Controller
@RequestMapping(value = "/owinfo") //给一个公共的访问地址
public class OwinfoController {
    @Autowired
    private IOwinfoService owinfoService;
    /**
     * 转向药品出入库页面
     */
    @RequestMapping
    public String owinfo(){
        return "/owinfo";
    }

    /**
     * 分页查询药品出入库列表
     * @param param
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/owinfoQueryPages")
    @ResponseBody
    public Object owinfoQueryPages(String param,
                                     @RequestParam(defaultValue = "1")int pageNum,  //为第1页
                                     @RequestParam(defaultValue = "10")int pageSize){ //每页数量为10条数据
        try {
            IPage<Owinfo> iPage = owinfoService.selectOwinfoPage(pageNum,pageSize,param);
            return ResultMapUtils.getHashMapMysqlPage(iPage);
        }catch (Exception e){
            return ResultMapUtils.hashMapException(e);
        }
    }

    /**
     * 转向药品出入库新增页面
     * @return
     */
    @RequestMapping(value = "/owinfoPage")
    public String owinfoPage(){
        return "/owinfoPage";
    }

    /**
     * 转向药品出入库编辑页面
     * 根据id查询到药品出入库，然后放入到model
     * @return
     */
    @RequestMapping(value = "/owinfoQueryById")
    public String owinfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        // 获取当前对象
        Owinfo owinfo = owinfoService.queryOwinfoById(id);
        // 放入到model
        model.addAttribute("obj",owinfo);
        return "/owinfoPage";
    }

    /**
     * 添加一个药品出入库
     * 需要从前端返回owinfo对象
     */
    @RequestMapping(value = "/owinfoAdd")
    @ResponseBody
    public Object owinfoAdd(Owinfo owinfo){
        try{
            // 添加数据
            int i = owinfoService.addOwinfo(owinfo);
            // 返回成功为1 不成功为0
            return ResultMapUtils.getHashMapSave(i);
        }catch (Exception e){
            return ResultMapUtils.hashMapException(e);
        }
    }
    /**
     * 修改一个药品出入库
     * 需要从前端返回owinfo对象
     */
    @RequestMapping(value = "/owinfoEdit")
    @ResponseBody
    public Object owinfoEdit(Owinfo owinfo){
        try{
            owinfo.setCreatetime(new Date());
            // 添加数据
            int i = owinfoService.editOwinfo(owinfo);
            // 返回成功为1 不成功为0
            return ResultMapUtils.getHashMapSave(i);
        }catch (Exception e){
            return ResultMapUtils.hashMapException(e);
        }
    }

    /**
     * 删除一个药品出入库
     * 需要从前端返回owinfo对象
     */
    @RequestMapping(value = "/owinfoDelById")
    @ResponseBody
    public Object owinfoDelById(Integer id){
        try{
            // 添加数据
            int i = owinfoService.delOwinfoById(id);
            // 返回成功为1 不成功为0
            return ResultMapUtils.getHashMapDel(i);
        }catch (Exception e){
            return ResultMapUtils.hashMapException(e);
        }
    }
}
